package com.qingmu.club.minaapi.custom.logic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingmu.club.minaapi.plus.entity.ClubUsersIntegral;
import com.qingmu.club.minaapi.plus.entity.MagicGroupJoinlist;
import com.qingmu.club.minaapi.plus.entity.MagicGroupRule;
import com.qingmu.club.minaapi.plus.entity.MagicGroupRun;
import com.qingmu.club.minaapi.plus.service.*;
import com.qingmu.club.minaapi.utils.KeyValueDefine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: duke
 * @Date: 2019-10-29 16:30
 */
@Component
public class MagicGroupLogic {

    @Autowired
    MagicGroupRunService magicGroupRunService;
    @Autowired
    MagicGroupService magicGroupService;
    @Autowired
    MagicGroupRuleService magicGroupRuleService;
    @Autowired
    ClubUsersIntegralService clubUsersIntegralService;
    @Autowired
    MagicGroupJoinlistService magicGroupJoinlistService;

    private final static Logger logger = LoggerFactory.getLogger(MagicGroupLogic.class);

    @Async
    public void addJf(MagicGroupRun run, MagicGroupRule rule, String shopId) {

        List<MagicGroupJoinlist> joins = this.magicGroupJoinlistService.list(new QueryWrapper<MagicGroupJoinlist>()
                .lambda()
                .eq(MagicGroupJoinlist::getRunId, run.getId())
        );

        for (MagicGroupJoinlist join : joins) {
            ClubUsersIntegral oneRecommend = new ClubUsersIntegral();
            oneRecommend.setIntegralName(String.format("%s拼团获取积分", join.getIsLeader() == 1 ? "团长" : "团员"));
            //oneRecommend.setClubUsersId(join.getUserId());
            oneRecommend.setUserId(join.getUserId());
            oneRecommend.setShopId(shopId);
            oneRecommend.setIntegralNum(join.getIsLeader() == 1 ? rule.getMasterJf() : rule.getMemberJf());//赠送二次分享积分
            oneRecommend.setType(1);
            oneRecommend.setIntegralType(KeyValueDefine.JF_AUTO_CHANGE);//系统自动操作
            oneRecommend.setIntegralChangeType(KeyValueDefine.SHARE_ATTENTION);
            this.clubUsersIntegralService.save(oneRecommend);
            logger.info(String.format("%s用户增加积分",join.getUserId()));
        }

    }
}
